package co.za.itweb.brainstormlite;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ContactHandler extends DefaultHandler 
{
	
	ContactFeed _feed;
	ContactList _item;
	String _lastElementName = "";
	
	final int RSS_ID = 1;
	final int RSS_CATID = 2;
	final int RSS_PRODID = 3;
	final int RSS_NAME = 4;
	final int RSS_DESCRIPTION = 5;
	final int RSS_IMAGE = 6;
	final int RSS_CATEGORY = 7;
	final int RSS_PRODUCER = 8;
	StringBuilder buf;
	int depth = 0;
	int currentstate = 0;
	
	/*
	 * Constructor 
	 */
	ContactHandler()
	{
	 buf =	new StringBuilder();
	}
	
	/*
	 * getFeed - this returns our feed when all of the parsing is complete
	 */
	ContactFeed getFeed()
	{
		return _feed;
	}
	
	
	public void startDocument() throws SAXException
	{
		// initialize our RSSFeed object - this will hold our parsed contents
		_feed = new ContactFeed();
		// initialize the RSSItem object - we will use this as a crutch to grab the info from the channel
		// because the channel and items have very similar entries..
		_item = new ContactList();

	}
	public void endDocument() throws SAXException
	{
	}
	public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
	{
		 buf =	new StringBuilder();
		depth++;
		if (localName.equals("channel"))
		{
			currentstate = 0;
			return;
		}
		
		if (localName.equals("item"))
		{
			// create a new item
			_item = new ContactList();
			return;
		}
		if (localName.equals("id"))
		{
			currentstate = RSS_ID;
			return;
		}
		if (localName.equals("cat_id"))
		{
			currentstate = RSS_CATID;
			return;
		}
		if (localName.equals("prod_id"))
		{
			currentstate = RSS_PRODID;
			return;
		}
		if (localName.equals("name"))
		{
			currentstate = RSS_NAME;
			return;
		}
		if (localName.equals("description"))
		{
			currentstate = RSS_DESCRIPTION;
			return;
		}
		
		if (localName.equals("image"))
		{
			currentstate = RSS_IMAGE;
			return;
		}
		
		if (localName.equals("category"))
		{
			currentstate = RSS_CATEGORY;
			return;
		}
		
		if (localName.equals("producer"))
		{
			currentstate = RSS_PRODUCER;
			return;
		}
		
		
		currentstate = 0;
	}
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException
	{
		depth--;
		if (localName.equals("item"))
		{
			// add our item to the list!
			_feed.addItem(_item);
			return;
		}
		
		switch (currentstate)
		{
			case RSS_ID:
				_item.setContactId(buf.toString());
				currentstate = 0;
				break;
			case RSS_CATID:
				_item.setCategoryId(buf.toString());
				currentstate = 0;
				break;
			case RSS_PRODID:
				_item.setProducerId(buf.toString());
				currentstate = 0;
				break;
			case RSS_NAME:
				_item.setContactName(buf.toString());
				currentstate = 0;
				break;
			case RSS_DESCRIPTION:
				_item.setContactDescription(buf.toString());
				currentstate = 0;
				break;
			case RSS_IMAGE:
				_item.setContactIcon(buf.toString());
				currentstate = 0;
				break;
			case RSS_CATEGORY:
				_item.setCategory(buf.toString());
				currentstate = 0;
				break;
				
			case RSS_PRODUCER:
				_item.setCompany(buf.toString());
				currentstate = 0;
				break;
			
			default:
				return;
		}
	}
	 
	public void characters(char ch[], int start, int length)
	{
		if (buf!=null) {
	        for (int i=start; i<start+length; i++) {
	            buf.append(ch[i]);
	        }
	    }
		
		
		
		
		
	}
}
